package AIbyDavid;

public class Experiment {
	private boolean useElitism;
	private boolean useTruncation;
	public Experiment(int pop, int popinc, int gen, boolean useTruncation, boolean useElitism, int rep){
		this.useElitism=useElitism;
		this.useTruncation=useTruncation;
		int counter=pop;
		while(true){
			counter=counter+popinc;
			simulation(counter,gen, rep);
		}
//		System.out.println("counter at the end of exc: "+ counter);
		
		
	}
	public int simulation(int populationSize, int generations, int repeatSimulationCount){
		long totalExecutionTime=0;
		int bestSolution=0;
		int worstSolution=Integer.MAX_VALUE;
		int nbrOfBadSolutions=0;
		for(int j=0;j<repeatSimulationCount;j++){
			//------------------------
			//The experiment
			long before = System.currentTimeMillis();
			Population2 p2 = new Population2(populationSize, useTruncation,useElitism);//population size must be a multiple of 2 because of the  modifyFitness function
			int max = 0;
			for (int i = 0; i < generations; i++) {
				// System.out.println(p2);
				int temp = p2.nextIteration();
				if (temp > max) {
					max = temp;
				}
			}
			long time = System.currentTimeMillis() - before;
			totalExecutionTime+=time;
			if(max>bestSolution){
				bestSolution=max;
			}
			if(max<worstSolution){
				worstSolution=max;
			}
			if(max!=983){
				nbrOfBadSolutions++;
			}
		}
		System.out.println("Simulation with pop: "+populationSize+", nbr of generations: "+generations+", reps: "+repeatSimulationCount);
		System.out.println("Average execution time: " + totalExecutionTime/repeatSimulationCount + "ms");
		System.out.println("Best solution: "+bestSolution);
		System.out.println("Worst solution: "+ worstSolution);
		System.out.println("Nbr of bad solutions: "+ nbrOfBadSolutions);
		System.out.println("------");
		return worstSolution;
	}

}
